package net.wanho.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wanho.dto.ProductDTO;
import net.wanho.dto.ProductMassDTO;
import net.wanho.mapper.*;
import net.wanho.po.Product;
import net.wanho.service.ProductService;
import net.wanho.util.ThreadUtil;
import net.wanho.vo.PageInfo;
import net.wanho.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【product(商品表)】的数据库操作Service实现
 * @createDate 2025-04-28 11:41:58
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {


    private final BrandMapper brandMapper;
    private final ProductTypeMapper productTypeMapper;
    private final SupplyMapper supplyMapper;
    private final PlaceMapper placeMapper;
    private final UnitMapper unitMapper;
    private final ProductMapper productMapper;
    private final StoreMapper storeMapper;


    public ProductServiceImpl(BrandMapper brandMapper, ProductTypeMapper productTypeMapper, SupplyMapper supplyMapper, PlaceMapper placeMapper, UnitMapper unitMapper, ProductMapper productMapper, StoreMapper storeMapper) {
        this.brandMapper = brandMapper;
        this.productTypeMapper = productTypeMapper;
        this.supplyMapper = supplyMapper;
        this.placeMapper = placeMapper;
        this.unitMapper = unitMapper;
        this.productMapper = productMapper;
        this.storeMapper = storeMapper;
    }

    @Override
    public PageInfo<ProductVo> productPageList(Integer pageNum, Integer pageSize, ProductDTO productDTO) {
        Page<Product> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!ObjectUtil.isEmpty(productDTO.getStoreId()), "store_id", productDTO.getStoreId());
        queryWrapper.like(!ObjectUtil.isEmpty(productDTO.getProductName()), "product_name", productDTO.getProductName());
        Integer brandId = brandMapper.selectIdByBrandName(productDTO.getBrandName());
        queryWrapper.eq(!ObjectUtil.isEmpty(brandId), "brand_id", brandId);
        Integer typeId = productTypeMapper.selectIdByTypeName(productDTO.getTypeName());
        queryWrapper.eq(!ObjectUtil.isEmpty(typeId), "type_id", typeId);
        Integer supplyId = supplyMapper.selectIdBySupplyName(productDTO.getSupplyName());
        queryWrapper.eq(!ObjectUtil.isEmpty(supplyId), "supply_id", supplyId);
        Integer placeId = placeMapper.selectIdByPlaceName(productDTO.getPlaceName());
        queryWrapper.eq(!ObjectUtil.isEmpty(placeId), "place_id", placeId);
        queryWrapper.eq(!ObjectUtil.isEmpty(productDTO.getUpDownState()), "up_down_state", productDTO.getUpDownState());


        // 安全地处理过期状态查询
        if (productDTO.getIsOverDate() != null) {
            if (productDTO.getIsOverDate() == 0) {
                queryWrapper.apply("supp_date > NOW()"); // 未过期的商品
            } else if (productDTO.getIsOverDate() == 1) {
                queryWrapper.apply("supp_date < NOW()"); // 已过期的商品
            }
        }

        Page<Product> p = this.baseMapper.selectByPage(page, queryWrapper);

        List<ProductVo> productVoList = new ArrayList<>();
        for (Product product : p.getRecords()) {
            ProductVo vo = new ProductVo();
            BeanUtils.copyProperties(product, vo);
            // 设置关联的名称
            if (product.getStoreId() != null) {
                vo.setStoreName(storeMapper.selectById(product.getStoreId()).getStoreName());
            }
            if (product.getBrandId() != null) {
                vo.setBrandName(brandMapper.selectById(product.getBrandId()).getBrandName());
            }
            if (product.getTypeId() != null) {
                vo.setTypeName(productTypeMapper.selectById(product.getTypeId()).getTypeName());
            }
            if (product.getSupplyId() != null) {
                vo.setSupplyName(supplyMapper.selectById(product.getSupplyId()).getSupplyName());
            }
            if (product.getPlaceId() != null) {
                vo.setPlaceName(placeMapper.selectById(product.getPlaceId()).getPlaceName());
            }
            if (product.getUnitId() != null) {
                vo.setUnitName(unitMapper.selectById(product.getUnitId()).getUnitName());
            }
            productVoList.add(vo);
        }

        PageInfo<ProductVo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getPages());
        pageInfo.setLimitIndex(page.offset());
        pageInfo.setResultList(productVoList);
        return pageInfo;
    }

    @Override
    public void add(Product product) {
        product.setUpDownState("0");
        LocalDateTime now = LocalDateTime.now();
        product.setCreateTime(now);
        Integer id = ThreadUtil.getId();
        product.setCreateBy(id);
        product.setThreshold(10);
        this.save(product);
    }

    @Override
    public void deleteById(Integer id) {
        this.baseMapper.deleteById(id);

    }

    @Override
    public void updateById(ProductMassDTO productMassDTO) {

        Product product = new Product();

        BeanUtils.copyProperties(productMassDTO, product);

        product.setUpdateTime(LocalDateTime.now());

        product.setUpdateBy(ThreadUtil.getId());

        this.baseMapper.updateById(product);


    }
}




